<?php
	header('Content-Type: text/html; charset=utf-8');
	setCookie("admin","benvi",time()+(99*24*3600),"/");
?>
<html>
<head>
<title>Hurmpf - Admin</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="JavaScript" src="../ajax.js"></script>
<script language="JavaScript" src="admin.js"></script>
<script language="JavaScript">window.name="admin";</script>
<?php if(isset($_GET['tinymce'])) { ?>
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins : "table,save,advimage,advlink,iespell,inlinepopups,preview,contextmenu,paste,fullscreen,visualchars,nonbreaking,xhtmlxtras,wordcount,media",
		theme_advanced_buttons1 : "undo,redo,|,bold,italic,underline,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,|,pastetext,|,link,unlink,anchor,image,media,charmap,nonbreaking,hr,|,bullist,numlist,|,outdent,indent,|,code,fullscreen",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		relative_urls: true,
		document_base_url: 'http://hurmpf.free.fr/',
		//document_base_url: 'http://localhost/hurmpf6s/',
		content_css : "style.css",
		force_br_newlines : true,
		extended_valid_elements : "iframe[src|width|height|name|align]"
	});
</script>
<?php } ?>
</head>
<body onLoad="listDraft()">

<?php
	include_once("../config.php"); // paramètres
	include_once("functions.php"); // fonctions de l'interface d'admin
	dbconnect();
	$editId = (!empty($_GET['id'])) ? $_GET['id'] : null;
	$tinymce = isset($_GET['tinymce']);
?>

<ul id="menu">
	<li><a href="../">Retour au blog</a></li>
	<li><a href="./">Nouveau post</a></li>
	<li><form method="get" action="index.php">
	<select name="id">
	<?php
		$sql = 'SELECT * FROM posts ORDER BY id DESC';
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
		if(mysql_numrows($req) > 0)
		{
			while($data = mysql_fetch_array($req)) 
			{
				$titre = substr($data['titre'],0,50);
				if(!empty($_GET['id']) && $data['id']==$_GET['id'])
					echo '<option selected value="'.$data['id'].'">'.$titre.'</option>';
				else
					echo '<option value="'.$data['id'].'">'.$titre.'</option>';
			}
		}
		else echo 'Aucun enregistrement';
	?>
	</select>
	<input type="submit" value="Editer">
	</form></li>
	<li><a href="./<?php echo (($editId || !$tinymce)?'?':'').($editId?('id='.$editId):'').(($editId && !$tinymce)?'&':'').($tinymce?'':'tinymce'); ?>">Changer d'éditeur</a></li>
</ul>

<hr>

<?php	
	if($editId==null)
	{
		echo '<form>';
		$date = date('Y-m-d H:i:s');
		$titre = "";
		$texte = "";
		if(!empty($_GET['titre']))
		{
			$titre = urldecode($_GET['titre']);
			if(!empty($_GET['texte'])) $texte = urldecode($_GET['texte']);
			if(!empty($_GET['url']))
			$texte .= "\n".'<a href="'.$_GET['url'].'">'.$_GET['url'].'</a>';
			// javascript:location.href="http://localhost/hurmpf5.1/admin/index.php?titre="+document.title+"&texte="+document.getSelection()+"&url="+location.href;
		}
	}
	else
	{
		echo '<form>';
		$sql = 'SELECT * FROM posts WHERE id='.$editId;
		$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
		$data = mysql_fetch_array($req);
		$date = $data['date'];
		$titre = $data['titre'];
		$texte = $data['texte'];
		$texte = str_replace("&","&amp;",$texte);
	}
	echo '<p>Titre : <input type="text" id="editTitle" maxlength="255" size="100" value="'.str_replace('"','&quot;',$titre).'">';
	echo '<p><textarea name="texte" rows="25" cols="120" id="editText">'.$texte.'</textarea>';
	echo '<p>Date : <input type="text" id="editDate" maxlength="100" size="50" value="'.$date.'">';
	
	if($editId==null)
	{
		echo ' <input type="button" value="Valider" onClick="addPost()">';
		echo ' <input type="button" value="Prévisualiser" onClick="previewPost()">';
		echo ' <input type="button" value="Brouillon" onClick="saveDraft()">';
		echo '<input type="hidden" id="draft" value="">';
	}
	else
	{
		echo ' <input type="button" value="Valider" onClick="editPost('.$editId.')">';
		echo ' <input type="button" value="Prévisualiser" onClick="previewPost()">';
		echo ' <input type="button" value="Effacer" onClick="deletePost('.$editId.')">';
	}
	echo '</form>';
?>

<hr>

<ul id="images">
</ul>

<hr>

<div id="uploadform">
<form action="upload.php" method="post" enctype="multipart/form-data" target="form_target" onSubmit="startUpload()">
	<input type="file" name="image" />
	<input type="checkbox" name="redim">Redim
	<input type="submit" value="Envoyer" />
	<div id="status" onClick="showLog()">Ready</div>
</form>
</div>
<!-- frame qui ne sert à rien qu'à lancer le formulaire tout en restant sur cette page -->
<iframe id="form_target" name="form_target" src="#"></iframe>

<div id="galleryform">
<form>
	<select id="galleryDir">
	<?php
		$list = listSubDirectories("../img/");
		foreach ($list as $dir)
			if(substr($dir,7,2)=="20")
				echo '<option value="'.$dir.'">'.substr($dir,7).'</option>';
	?>
	</select>
	<input type="button" value="Afficher" onClick="showGallery()"/>
</form>
</div>

<hr>

<div id="draftlist"></div>
<?php mysql_close(); ?>
</body>
</html>